//
//  ChangePhoneTwoViewController.m
//  NewPro
//
//  Created by RYX on 2019/4/26.
//  Copyright © 2019 何荧红. All rights reserved.
//

#import "ChangePhoneTwoViewController.h"
#import "ChangePhoneSuccessViewController.h"
#import "PutForwardRecordStore.h"
#import "SetStore.h"
#import "UserDefaultsManager.h"
#import "MineStore.h"
#import "UserModel.h"

@interface ChangePhoneTwoViewController ()
{
    NSString *userphone;
}
@property (weak, nonatomic) IBOutlet UITextField *phonetf;
@property (weak, nonatomic) IBOutlet UITextField *codetf;

@end

@implementation ChangePhoneTwoViewController

- (void)viewWillAppear:(BOOL)animated{
    
    [super viewWillAppear:animated];
    
    [self getUserInfo];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setNavBarWithTitle:@"验证手机号码"];
    _phonetf.keyboardType = UIKeyboardTypeNumberPad;
    _phonetf.clearButtonMode = UITextFieldViewModeWhileEditing;
    _codetf.keyboardType = UIKeyboardTypeNumberPad;
    _codetf.clearButtonMode = UITextFieldViewModeWhileEditing;
    
}

- (void)getUserInfo{
    
    MineStore *store = [[MineStore alloc] init];
    
    [store getUserInfoWithCustomId:[UserDefaultsManager sharedInstance].user_id Success:^(UserModel *model) {
        
        self->userphone = model.phoneNumber;
        
    } Failure:^(NSError *error) {
    
    }];
}
#pragma mark - 获取验证码
- (IBAction)onCodeBtn:(UIButton *)sender {
    
    if (_phonetf.text.length <= 0) {
        
        return [self showMBPError:@"请输入新手机号"];
    }
    
    if (![_phonetf.text isVaildMobileNo]) {
        
        return [self showMBPError:@"请输入正确手机号"];
    }
    
    if ([_phonetf.text isEqualToString:userphone]) {
        
        return [self showMBPError:@"手机号与默认手机号相同"];
    }
    
    sender.enabled = NO;
    
    MJWeakSelf
    PutForwardRecordStore *store = [[PutForwardRecordStore alloc] init];
    [store getCodeWithPhone:_phonetf.text Success:^{
            
        __block int n = 60;
        dispatch_source_t timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, dispatch_get_main_queue());
        dispatch_source_set_timer(timer, DISPATCH_TIME_NOW, 1.0 * NSEC_PER_SEC, 0);
        dispatch_source_set_event_handler(timer, ^{
            
            --n;
            NSLog(@"%i", n);
            [sender setTitle:[NSString stringWithFormat:@"%ds",n] forState:UIControlStateNormal];
            [sender setTitleColor:UIColor.whiteColor forState:UIControlStateNormal];
            sender.backgroundColor = BLUECOLOR;
            if (0 == n) {
                dispatch_source_cancel(timer);
            }
        });
        // 计时器cancel的时候触发的block
        dispatch_source_set_cancel_handler(timer, ^{
            
            sender.enabled = YES;
            NSLog(@"cancel");
            [sender setTitle:@"重新获取" forState:UIControlStateNormal];
            [sender setTitleColor:BLUECOLOR forState:UIControlStateNormal];
            sender.backgroundColor = UIColor.whiteColor;
            
        });
        dispatch_resume(timer);
        
    } Failure:^(NSError *error) {
        
        sender.enabled = YES;
        [weakSelf showMBPError:[HttpTool handleError:error]];
    }];
    
}

#pragma mark - 确定
- (IBAction)onSureBtn:(UIButton *)sender {
    
    if (_phonetf.text.length <= 0) {
        
        return [self showMBPError:@"请输入新的手机号"];
    }
    
    if (_codetf.text.length <= 0) {
        
        return [self showMBPError:@"请输入验证码"];
    }
    
    if (![_phonetf.text isVaildMobileNo]) {
        
        return [self showMBPError:@"请输入正确手机号"];
    }
    
    sender.enabled = NO;
    
    MJWeakSelf
    SetStore *store = [[SetStore alloc] init];
    [store changePhoneNumWithNewPhone:[_phonetf.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] code:_codetf.text Success:^{
        
        [[NSUserDefaults standardUserDefaults] setObject:[self->_phonetf.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] forKey:@"phoneNumber"];
        
        [UserDefaultsManager sharedInstance].user_mobile = [self->_phonetf.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
        
        sender.enabled = YES;
        ChangePhoneSuccessViewController *VC = [[ChangePhoneSuccessViewController alloc] init];
        [weakSelf.navigationController pushViewController:VC animated:YES];
        
    } Failure:^(NSError *error) {
        
        sender.enabled = YES;
        [weakSelf showMBPError:[HttpTool handleError:error]];
    }];
}

@end
